﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Utils;
using PanasonicWarranty.DAL;
namespace PanasonicWarranty.Business
{
    public partial class pa_Model
    {
    	#region public properties
		[Required(ErrorMessage="Model Code Required")]
		public string ModelCode { get; set; }
		[Required(ErrorMessage="Cate Code Required")]
		public string CateCode { get; set; }
		public int? WarrantyPeriod { get; set; }
		public bool? Disabled { get; set; }
		#endregion

		#region constructor
		public pa_Model(){ }

		public pa_Model(IDataReader rd)
		{
			this.ModelCode = rd["ModelCode"].ToString();
			this.CateCode = rd["CateCode"].ToString();
			this.WarrantyPeriod = rd["WarrantyPeriod"].Equals(DBNull.Value) ? (int?)null : (int)rd["WarrantyPeriod"];
			this.Disabled = rd["Disabled"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Disabled"];
		}
		#endregion

		#region private method
        private bool Create()
        {
            return DBpa_Model.Create(ModelCode, CateCode, WarrantyPeriod, Disabled);
        }

        private bool Update()
        {
            return DBpa_Model.Update(ModelCode, CateCode, WarrantyPeriod, Disabled);
        }
		#endregion

		#region public method
		public bool Save()
		{
            if(ModelCode != string.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }

            if(CateCode != string.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
		}
		#endregion

		#region static method
        public static bool Delete(string ModelCode, string CateCode)
        {
            return DBpa_Model.Delete(ModelCode, CateCode);
        }

        public static List<pa_Model> GetAll()
        {
            return ObjectClass.CreateList<pa_Model>(DBpa_Model.GetAll());
        }

        public static pa_Model GetByKey(string ModelCode, string CateCode)
        {
            return ObjectClass.CreateInstance<pa_Model>(DBpa_Model.GetByKey(ModelCode, CateCode));
        }

        public static List<pa_Model> GetByForeignKeys(string CateCode)
        {
            return ObjectClass.CreateList<pa_Model>(DBpa_Model.GetByForeignKeys(CateCode));
        }

        public static List<pa_Model> GetByTop(string top, string where, string order)
        {
            return ObjectClass.CreateList<pa_Model>(DBpa_Model.GetByTop(top, where, order));
        }
		#endregion
	}
}
